package com.wyx.wyxmember;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;
import org.springframework.data.redis.core.StringRedisTemplate;
import java.util.List;
import com.wyx.wyxmember.message.Sender;

@RestController
public class MemberController {
    @Value("${server.port}")
    private String serverPort;
    private static int count = 0;

    @Autowired
    private StringRedisTemplate stringRedisTemplate;

    @Autowired
    private RedisTemplate redisTemplate;
    @Autowired
    UserDao userDao;

    @Autowired
    PersonDao personDao;

    @Autowired
    private Sender sender;

    @RequestMapping("/getMemberAll")
    public List<String> getMemberAll() {
        try{
            Thread.sleep(3000);
        }catch (Exception e){

        }
        count++;
       // List<String> listUser = new ArrayList<String>();
       // listUser.add("zhangsan");
       // listUser.add("lisi");
       // listUser.add("cmx");
       // listUser.add("count"+count);
        //System.out.println("count:" + count);
       // listUser.add("serverPort:"+serverPort);
        return null;
    }

    @RequestMapping(value = "/create",produces="text/html;charset=UTF-8",method = RequestMethod.GET)
    public String create(String email, String name) {
        String userId = "";
        try {
            User user = new User();
            user.setName(name);
            user.setEmail(email);
           userDao.save(user);
           userId = String.valueOf(user.getId());

           Person person = new Person();
           person.setAge(4);
            personDao.save(person);
            //redisTemplate.opsForValue().set("name", "wyx", 20);
            stringRedisTemplate.opsForValue().set("age", "12");
            stringRedisTemplate.opsForValue().set("name", "wyx");

            sender.send();
        }
        catch (Exception ex) {
            return "Error creating the user: " + ex.toString();
        }
        return "User succesfully created with id = " + userId;
    }


    @RequestMapping("/getMemberServiceApi")
    public String getMemberServiceApi(){
        return "this is 会员服务工程";
    }



    @PutMapping(value = "/put")
    public void put(String key, @RequestParam(required = false, defaultValue = "default") String value) {
        stringRedisTemplate.opsForValue().set(key, value);
    }

    @GetMapping(value = "/get")
    public Object get(String key) {
        return stringRedisTemplate.opsForValue().get(key);
    }
}
